package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.bo.GcjxljjtbgBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.annotation.FIllBasicInfoForTaiZhang;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjPhbsjsyjcbgBsInfoBo;
import com.ruoyi.report_sj.domain.vo.ReportSjPhbsjsyjcbgBsInfoVo;
import com.ruoyi.report_sj.domain.ReportSjPhbsjsyjcbgBsInfo;
import com.ruoyi.report_sj.mapper.ReportSjPhbsjsyjcbgBsInfoMapper;
import com.ruoyi.report_sj.service.IReportSjPhbsjsyjcbgBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆配合比设计试验检测报告Service业务层处理
 *
 * @author zyb
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class ReportSjPhbsjsyjcbgBsInfoServiceImpl implements IReportSjPhbsjsyjcbgBsInfoService {

    private final ReportSjPhbsjsyjcbgBsInfoMapper baseMapper;

    /**
     * 查询砂浆配合比设计试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportSjPhbsjsyjcbgBsInfoVo queryById(Long excelId){
        ReportSjPhbsjsyjcbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        if(StrUtil.isEmpty(vo.getJiancjl()) || "检测结论：".equals(vo.getJiancjl())){
            return jcjl(vo);
        }
        return vo;
    }

    @Override
    @FIllBasicInfoForTaiZhang
    public ReportSjPhbsjsyjcbgBsInfoVo queryByIdForTaizhang(Long excelId) {
        ReportSjPhbsjsyjcbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        if(StrUtil.isEmpty(vo.getJiancjl()) || "检测结论：".equals(vo.getJiancjl())){
            return jcjl(vo);
        }
        return vo;
    }

    /**
     * 查询砂浆配合比设计试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportSjPhbsjsyjcbgBsInfoVo> queryPageList(ReportSjPhbsjsyjcbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjPhbsjsyjcbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSjPhbsjsyjcbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆配合比设计试验检测报告列表
     */
    @Override
    public List<ReportSjPhbsjsyjcbgBsInfoVo> queryList(ReportSjPhbsjsyjcbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportSjPhbsjsyjcbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjPhbsjsyjcbgBsInfo> buildQueryWrapper(ReportSjPhbsjsyjcbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjPhbsjsyjcbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSjPhbsjsyjcbgBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSjPhbsjsyjcbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportSjPhbsjsyjcbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShipqd()), ReportSjPhbsjsyjcbgBsInfo::getShipqd, bo.getShipqd());
        lqw.eq(StringUtils.isNotBlank(bo.getShejcd()), ReportSjPhbsjsyjcbgBsInfo::getShejcd, bo.getShejcd());
        lqw.eq(StringUtils.isNotBlank(bo.getShuinqddj()), ReportSjPhbsjsyjcbgBsInfo::getShuinqddj, bo.getShuinqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getShuinkyqd()), ReportSjPhbsjsyjcbgBsInfo::getShuinkyqd, bo.getShuinkyqd());
        lqw.eq(StringUtils.isNotBlank(bo.getXijlmc()), ReportSjPhbsjsyjcbgBsInfo::getXijlmc, bo.getXijlmc());
        lqw.eq(StringUtils.isNotBlank(bo.getXijdjmd()), ReportSjPhbsjsyjcbgBsInfo::getXijdjmd, bo.getXijdjmd());
        lqw.eq(bo.getSsyrq() != null, ReportSjPhbsjsyjcbgBsInfo::getSsyrq, bo.getSsyrq());
        lqw.eq(bo.getQsyrq() != null, ReportSjPhbsjsyjcbgBsInfo::getQsyrq, bo.getQsyrq());
        lqw.eq(bo.getEsyrq() != null, ReportSjPhbsjsyjcbgBsInfo::getEsyrq, bo.getEsyrq());
        lqw.eq(bo.getZhijrq() != null, ReportSjPhbsjsyjcbgBsInfo::getZhijrq, bo.getZhijrq());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportSjPhbsjsyjcbgBsInfo::getJiancjl, bo.getJiancjl());
        return lqw;
    }

    /**
     * 新增砂浆配合比设计试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportSjPhbsjsyjcbgBsInfoBo bo) {
        ReportSjPhbsjsyjcbgBsInfo add = BeanUtil.toBean(bo, ReportSjPhbsjsyjcbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改砂浆配合比设计试验检测报告
     */
    @Override
    public Boolean updateByBo(ReportSjPhbsjsyjcbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportSjPhbsjsyjcbgBsInfo update = BeanUtil.toBean(bo, ReportSjPhbsjsyjcbgBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void fillExcelResult(ReportSjPhbsjsyjcbgBsInfoBo bo) {
//        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
//            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
//                if ("不符合".equals(detailVo.getJiegpd())) {
//                    bo.setExcelResult(2);
//                    return;
//                }
//            }
            bo.setExcelResult(1);
//        }
//        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjPhbsjsyjcbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆配合比设计试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ReportSjPhbsjsyjcbgBsInfoVo jcjl(ReportSjPhbsjsyjcbgBsInfoVo vo) {
//        检测结论：经检测，该水泥砂浆拌和物的分层度实值为15mm，其他所检参数符合JGJ/T98-2010的技术要求。
        vo.setJiancjl("检测结论：经检测，该水泥砂浆拌和物的分层度实值为"+vo.getFencd()!=null?vo.getFencd():"-"+"mm,其他所检参数符合JGJ/T98-2010的技术要求。");
        return vo;
    }
}
